home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 4ESS52 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  4.6 KB  |  104 lines

  1. package com.sun.java.swing.text;
  2.  
  3. import com.sun.java.swing.plaf.TextUI;
  4. import java.awt.Graphics;
  5. import java.awt.Insets;
  6. import java.awt.Rectangle;
  7. import java.util.Vector;
  8.  
  9. public class DefaultHighlighter implements Highlighter {
  10.    private Vector highlights = new Vector();
  11.    private JTextComponent component;
  12.  
  13.    public Object addHighlight(int p0, int p1, Highlighter.HighlightPainter p) throws BadLocationException {
  14.       Document doc = this.component.getDocument();
  15.       TextUI mapper = this.component.getUI();
  16.       HighlightInfo i = new HighlightInfo(this);
  17.       i.painter = p;
  18.       i.p0 = doc.createPosition(p0);
  19.       i.p1 = doc.createPosition(p1);
  20.       this.highlights.addElement(i);
  21.       mapper.damageRange(p0, p1);
  22.       return i;
  23.    }
  24.  
  25.    public void changeHighlight(Object tag, int p0, int p1) throws BadLocationException {
  26.       TextUI mapper = this.component.getUI();
  27.       Document doc = this.component.getDocument();
  28.       HighlightInfo info = (HighlightInfo)tag;
  29.       int oldP0 = info.p0.getOffset();
  30.       int oldP1 = info.p1.getOffset();
  31.       if (p0 == oldP0) {
  32.          mapper.damageRange(Math.min(oldP1, p1), Math.max(oldP1, p1));
  33.       } else if (p1 == oldP1) {
  34.          mapper.damageRange(Math.min(p0, oldP0), Math.max(p0, oldP0));
  35.       } else {
  36.          mapper.damageRange(oldP0, oldP1);
  37.          mapper.damageRange(p0, p1);
  38.       }
  39.  
  40.       info.p0 = doc.createPosition(p0);
  41.       info.p1 = doc.createPosition(p1);
  42.    }
  43.  
  44.    public void deinstall(JTextComponent c) {
  45.       this.component = null;
  46.    }
  47.  
  48.    public Highlighter.Highlight[] getHighlights() {
  49.       Highlighter.Highlight[] h = new Highlighter.Highlight[this.highlights.size()];
  50.       this.highlights.copyInto(h);
  51.       return h;
  52.    }
  53.  
  54.    public void install(JTextComponent c) {
  55.       this.component = c;
  56.       this.removeAllHighlights();
  57.    }
  58.  
  59.    public void paint(Graphics g) {
  60.       Rectangle a = new Rectangle(this.component.getSize());
  61.       Insets insets = this.component.getInsets();
  62.       a.x += insets.left;
  63.       a.y += insets.top;
  64.       a.width -= insets.left + insets.right;
  65.       a.height -= insets.top + insets.bottom;
  66.       int len = this.highlights.size();
  67.  
  68.       for(int i = 0; i < len; ++i) {
  69.          HighlightInfo info = (HighlightInfo)this.highlights.elementAt(i);
  70.          Highlighter.HighlightPainter p = info.getPainter();
  71.          p.paint(g, info.getStartOffset(), info.getEndOffset(), a, this.component);
  72.       }
  73.  
  74.    }
  75.  
  76.    public void removeAllHighlights() {
  77.       TextUI mapper = this.component.getUI();
  78.       if (mapper != null) {
  79.          int len = this.highlights.size();
  80.          if (len != 0) {
  81.             int p0 = Integer.MAX_VALUE;
  82.             int p1 = 0;
  83.  
  84.             for(int i = 0; i < len; ++i) {
  85.                HighlightInfo info = (HighlightInfo)this.highlights.elementAt(i);
  86.                p0 = Math.min(p0, info.p0.getOffset());
  87.                p1 = Math.max(p1, info.p1.getOffset());
  88.             }
  89.  
  90.             mapper.damageRange(p0, p1);
  91.             this.highlights.removeAllElements();
  92.          }
  93.       }
  94.  
  95.    }
  96.  
  97.    public void removeHighlight(Object tag) {
  98.       TextUI mapper = this.component.getUI();
  99.       HighlightInfo info = (HighlightInfo)tag;
  100.       mapper.damageRange(info.p0.getOffset(), info.p1.getOffset());
  101.       this.highlights.removeElement(tag);
  102.    }
  103. }
  104.